package financial.mock;

import net.datafaker.Faker;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

public class DataGenerator {
    private static final Faker faker = new Faker(new Locale("zh-CN"));
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    // 生成客户信息
    public static String[] generateCustomer(String dt) {
        return new String[] {
                UUID.randomUUID().toString(),
                faker.name().fullName(),
                generateFakeIdCard(),
                faker.phoneNumber().cellPhone(),
                faker.address().fullAddress(),
                randomTime("2020-01-01", "2023-01-01", TIMESTAMP_FORMAT),
                randomTime("2023-01-01", "2023-12-31", TIMESTAMP_FORMAT),
                dt
        };
    }

    // 生成账户信息（需传入客户ID）
    public static String[] generateAccount(String customerId, String dt) {
        return new String[] {
                "ACCT" + System.nanoTime() + ThreadLocalRandom.current().nextInt(1000, 9999),
                customerId,
                faker.options().option("储蓄账户", "对公账户"),
                String.format("%.2f", faker.number().randomDouble(2, 1000, 1000000)),
                randomDate("2010-01-01", "2023-01-01", DATE_FORMAT),
                faker.options().option("正常", "冻结"),
                randomTime("2023-01-01", "2023-12-31", TIMESTAMP_FORMAT),
                dt
        };
    }

    // 生成交易流水（需传入账户和交易编码）
    public static String[] generateTransaction(String accountNo, String transCode, String dt) {
        return new String[] {
                "TX" + System.currentTimeMillis() + ThreadLocalRandom.current().nextInt(1000, 9999),
                accountNo,
                transCode,
                String.format("%.2f", faker.number().randomDouble(2, 10, 50000)),
                faker.currency().code(),
                "ACCT" + faker.number().digits(10),
                randomTime("2023-01-01", "2023-12-31", TIMESTAMP_FORMAT),
                faker.options().option("成功", "失败"),
                faker.options().option("ATM", "网银", "手机银行"),
                dt
        };
    }

    // 生成假身份证号
    private static String generateFakeIdCard() {
        return faker.idNumber().valid();
    }

    // 随机日期生成
    private static String randomDate(String start, String end, DateTimeFormatter formatter) {
        LocalDateTime startDate = LocalDateTime.parse(start + "T00:00:00");
        LocalDateTime endDate = LocalDateTime.parse(end + "T00:00:00");
        long randomDay = ThreadLocalRandom.current().nextLong(
                startDate.toEpochSecond(java.time.ZoneOffset.UTC),
                endDate.toEpochSecond(java.time.ZoneOffset.UTC)
        );
        return LocalDateTime.ofEpochSecond(randomDay, 0, java.time.ZoneOffset.UTC)
                .format(formatter);
    }

    // 随机时间生成
    private static String randomTime(String start, String end, DateTimeFormatter formatter) {
        LocalDateTime startTime = LocalDateTime.parse(start + "T00:00:00");
        LocalDateTime endTime = LocalDateTime.parse(end + "T23:59:59");
        long randomSeconds = ThreadLocalRandom.current().nextLong(
                startTime.toEpochSecond(java.time.ZoneOffset.UTC),
                endTime.toEpochSecond(java.time.ZoneOffset.UTC)
        );
        return LocalDateTime.ofEpochSecond(randomSeconds, 0, java.time.ZoneOffset.UTC)
                .format(formatter);
    }
}